<!-- 猜你喜欢 -->
<template>
  <section class="product-section">
    <div class="box-hd">
      <div class="title">猜你喜欢</div>
      <div class="more">
        <router-link to="/goods">查看更多 ></router-link>
      </div>
    </div>
    <div class="box-bd">
      <div class="promo-list">
        <router-link to="/goods" class="promo-card recommend">
          <div class="promo-icon-wrapper">
            <i class="el-icon-star-on main-icon"></i>
            <i class="el-icon-goods tag-icon"></i>
          </div>
          <div class="promo-content">
            <h3>为您推荐</h3>
            <p>猜你喜欢 · 个性化推荐</p>
          </div>
        </router-link>
      </div>
      <div class="list" v-loading="loading">
        <MyList
          v-if="recommendList.length > 0"
          :list="recommendList"
          :isMore="false"
        ></MyList>
        <div v-else-if="!loading" class="empty-list">暂无推荐商品</div>
        <div v-else style="min-height: 300px"></div>
      </div>
    </div>
  </section>
</template>

<script>
import MyList from "./MyList.vue"; // 导入 MyList 组件

export default {
  name: "RecommendList",
  components: {
    MyList,
  },
  data() {
    return {
      recommendList: [], // 推荐商品列表
      loading: false, // 加载状态
    };
  },
  created() {
    this.getRecommend(); // 组件创建时获取推荐数据
  },

  methods: {
    getRecommend() {
      this.loading = true;
      this.$axios
        .post("/api/product/recommend", {
          user_id: this.$store.getters.getUser.user_id, // 使用正确的字段名 user_id
        })
        .then((res) => {
          if (res.data.code === "001") {
            // 确保后端返回的数据结构与 MyList 兼容
            // 例如: [{ product_id, product_name, product_picture, product_selling_price }]
            this.recommendList = res.data.data;
          } else {
            console.error("获取推荐商品失败:", res.data.msg);
            // this.$message.error(`获取推荐商品失败: ${res.data.msg}`);
          }
        })
        .catch((err) => {
          console.error("获取推荐商品异常:", err);
          // this.$message.error('获取推荐商品异常');
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style scoped>
.product-section {
  width: 100%;
  max-width: 1226px;
  margin: 0 auto;
  background-color: #fafafa;
  padding-bottom: 20px;
  margin-top: 20px;
}

.box-hd {
  position: relative;
  height: 58px;
  padding: 0 10px;
  background-color: #fff;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.box-hd .title {
  margin: 0;
  font-size: 22px;
  font-weight: 200;
  line-height: 58px;
  color: #333;
}

.box-hd .more {
  font-size: 16px;
  line-height: 58px;
}

.box-hd .more a {
  color: #424242;
  transition: color 0.3s;
}

.box-hd .more a:hover {
  color: #ff6700;
}

.box-bd {
  display: flex;
  padding: 10px;
  background-color: #fafafa;
}

.promo-list {
  width: 234px;
  margin-right: 14px;
}

.promo-card {
  display: block;
  width: 100%;
  height: 300px;
  padding: 30px 20px;
  text-align: center;
  background-color: #fff;
  transition: all 0.3s;
  border: none;
  text-decoration: none;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  color: inherit;
}

.promo-card.recommend {
  background: linear-gradient(135deg, #fff 0%, #f8f9fa 100%);
  color: #333;
}

.promo-icon-wrapper {
  position: relative;
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
}

.main-icon {
  font-size: 60px;
  color: #ff6b6b;
  background: rgba(255, 107, 107, 0.1);
  padding: 15px;
  border-radius: 50%;
}

.tag-icon {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 24px;
  color: #ff6b6b;
  background: #fff;
  padding: 6px;
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.promo-content h3 {
  margin-top: 20px;
  font-size: 24px;
  font-weight: 400;
}

.promo-content p {
  margin-top: 10px;
  font-size: 14px;
  opacity: 0.8;
}

.promo-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
}

.list {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  min-height: 340px;
}

.list >>> .my-list-item {
  width: calc(25% - 12px);
  margin-bottom: 14px;
  box-sizing: border-box;
}

.empty-list {
  width: 100%;
  text-align: center;
  line-height: 340px;
  color: #b0b0b0;
  font-size: 16px;
}

@media screen and (max-width: 1200px) {
  .list >>> .my-list-item {
    width: calc(33.33% - 10px);
  }
}

@media screen and (max-width: 992px) {
  .promo-list {
    display: none;
  }
}

@media screen and (max-width: 768px) {
  .list >>> .my-list-item {
    width: calc(50% - 8px);
  }
}

@media screen and (max-width: 480px) {
  .list >>> .my-list-item {
    width: 100%;
  }
}
</style>